<template>
    <div style="padding: 10px">
        <!-- 功能区 -->
        <div style="margin: 10px 0">
            <el-button type="primary" @click ="add">新增</el-button>
            <el-button type="primary">导入</el-button>
            <el-button type="primary">导出</el-button>
        </div>
        <!-- 搜索区 -->
        <div style="margin: 10px 0">
            <el-input v-model="search" style="width: 240px" placeholder="请输入用户名" clearable/>
            <el-button type="primary" style="margin: 5px" @click="load">查询</el-button>
        </div>
        <!-- 数据显示区 -->
        <el-table :data="tableData" border style="width: 100%">
            <el-table-column prop="id" label="id" sortable />
            <el-table-column prop="username" label="用户名"  />
            <el-table-column prop="role" label="角色" />
        <!-- 删除和编辑操作 -->
            <el-table-column fixed="right" label="操作" min-width="120">
                <template #default = "scope">

                    <el-button link type="test" size="small" @click="handleEdit(scope.row)">编辑</el-button>

                    <el-popconfirm title="确定删除?" @confirm="handleDelete(scope.row.id)">
                        <template #reference>
                            <el-button link type="test" size="small" >删除</el-button>
                        </template>
                    </el-popconfirm>
                    
                </template>
            </el-table-column>
        </el-table>
        
        <div style="margin: 10px 0">
        <!-- 分页 -->
            <el-pagination
            :current-page="currentPage"
            :page-size="pageSize"
            :page-sizes="[5, 10, 20]"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"/>
        <!-- 新增/更新 弹窗 -->
            <el-dialog
                v-model="dialogVisible"
                title="信息"
                width="500">
            <!-- 表单 -->
                <el-form :model="form" label-width = "120px">
                    <el-form-item label="用户名">
                        <el-input v-model="form.username" style="width: 80%"/>
                    </el-form-item>
                </el-form>

                <el-form :model="form" label-width = "120px">
                    <el-form-item label="角色">
                        <el-input v-model="form.role" style="width: 80%"/>
                    </el-form-item>
                </el-form>

                <template #footer>
                <div class="dialog-footer">
                    <el-button @click="dialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="save">确认</el-button>
                </div>
                </template>
            </el-dialog>

        </div>
    </div>
</template>

<script>
import request from '@/utils/request';

export default {
    name: 'Home',
    components: {

    },

    data() {
        return {
            //表单
            form: {},
            //控制弹窗
            dialogVisible: false,
            //搜索
            search: '',
            //分页
            currentPage: 1,
            pageSize: 10,
            total: 0,
            //数据
            tableData:[]
        }
    },
    //页面加载
    created() {
        this.load()
    },
    methods: {
        //分页查询
        load() {
            request.get("/user",{
                params: {
                pageNum: this.currentPage,
                pageSize: this.pageSize,
                search: this.search
            },
            }).then(res => {
                console.log(res)
                this.tableData = res.data.records
                this.total = res.data.total
            })
        },
        //新增
        add() {
            this.dialogVisible = true
            this.form = {}//清空表单操作
        },
        //表单信息传到后端
        save() {
            if (this.form.id) { //更新
                request.put("/user",this.form).then(res =>{
                    if (res.code == "0"){
                        this.$message({
                            type:"success",
                            message:"更新成功"
                        })
                    } else {
                        this.$message({
                            type:"error",
                            message :res.msg
                        })
                    }
                    this.load() //刷新表格数据
                    this.dialogVisible = false
                })
            } else { //新增
            request.post("/user",this.form).then(res =>{
                if (res.code == "0"){
                        this.$message({
                            type:"success",
                            message:"插入成功"
                        })
                    } else {
                        this.$message({
                            type:"error",
                            message :res.msg
                        })
                    }
                    this.load() //刷新表格数据
                    this.dialogVisible = false
            })
            }

        },
        //编辑
        handleEdit(row) {
            this.form = JSON.parse(JSON.stringify(row))
            this.dialogVisible = true
        },
        //删除
        handleDelete(id){
            request.delete("/user/" + id).then(res =>{
                if (res.code == "0"){
                        this.$message({
                            type:"success",
                            message:"删除成功"
                        })
                    } else {
                        this.$message({
                            type:"error",
                            message :res.msg
                        })
                    }
                    this.load() //刷新表格数据
            })
        },
        // 分页
        handleSizeChange(pageSize) {
            this.pageSize = pageSize
            this.load()
        },
        handleCurrentChange(pageNum) {
            this.currentPage = pageNum
            this.load()
        }
        
    }
}
</script>